<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品类
 * @package  module/base/goods
 * @author   allen.qiang
 * @version 1.0
 */
   
class quanliGoods {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }


    /**
     * 查询二级商品信息
     * @param array $arg_array 查询参数数组
     * @return null|array
     */
    public function searchList( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //二级商品网购号
        if (isset($arg_array['postID']) && quanli_is_int($arg_array['postID'])) {
            $sql .= ' AND postID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['postID'];
        }
        
        //分类
        if (isset($arg_array['bigCategoryID']) && !empty($arg_array['bigCategoryID'])) {
            $sql .= ' AND bigCategoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['bigCategoryID'];
        }

        //城市ID
        if (isset($arg_array['cityID']) && quanli_is_int($arg_array['cityID'])) {
            $sql .= ' AND cityID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['cityID'];
        }

        //商品SKU
        if (isset($arg_array['product_code']) && !empty($arg_array['product_code'])) {
            $sql .= ' AND product_code = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['product_code'];
        }

        //商品名称
        if (isset($arg_array['goodsName']) && !empty($arg_array['goodsName'])) {
            $sql .= ' AND goodsName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['goodsName'] . '%';
        }

        //商品编号
        if (isset($arg_array['mainPostID']) && !empty($arg_array['mainPostID'])) {
            $sql .= ' AND mainPostID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['mainPostID'];
        }

        //上下架
        if (isset($arg_array['useFlg']) && $arg_array['useFlg'] != 'all' &&($arg_array['useFlg'] == 0 || $arg_array['useFlg'] == 1)) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //销售
        if (isset($arg_array['forsale']) && in_array($arg_array['forsale'], array(1, 2))) {
            $sql .= ' AND forsale = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['forsale'];
        }

        //是否可以试吃
        if (isset($arg_array['foretaste']) && in_array($arg_array['foretaste'], array(0, 1))) {
            $sql .= ' AND foretaste = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['foretaste'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_list ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM goods_list ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            $item_array = array();
            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'goodsName' => $item->goodsName,
                    'frenchName' => $item->frenchName,
                    'mainPostID' => $item->mainPostID,
                    'postID' => $item->postID,
                    'goodsID' => $item->goodsID,
                    'product_code' => $item->product_code,
                    'bigCategoryID' => $item->bigCategoryID,
                    'bigCategoryName' => $item->bigCategoryName,
                    'midCategoryID' => $item->midCategoryID,
                    'midCategoryName' => $item->midCategoryName,
                    'smallCategoryID' => $item->smallCategoryID,
                    'smallCategoryName' => $item->smallCategoryName,
                    'useFlg' => $item->useFlg,
                    'forsale' => $item->forsale,
                    'foretaste' => $item->foretaste,
                    'spec' => $item->spec,
                    'tag' => $item->tag,
                    'marketPrice' => $item->marketPrice,
                    'salePrice' => $item->salePrice,
                    'discountFlg' => $item->discountFlg,
                    'chineseBrief' => $item->chineseBrief,
                    'frenchBrief' => $item->frenchBrief,
                    'goodsMainPic' => set_goods_img_link( $item->goodsMainPic ),
                    'img_link' => set_goods_img_link( $item->goodsMainPic ),
                    'link' => set_goods_link( $item->uid ),
                    'wap_link' => set_goods_link( $item->uid, 2 ),
                    'uid' => $item->uid,
                    'isPublish' => $item->isPublish,
                    'cityID' => $item->cityID,
                    'cityName' => $item->cityName,
                    'goodsPicTitle' => $item->goodsPicTitle,
                    'goodsPicAlt' => $item->goodsPicAlt,
                    'sortOrder' => $item->sortOrder,
                    'saletotal' => $item->saletotal,
                    'tagID' => $item->tagID,
                    'tagName' => $item->tagName,
                    'tagImg' => $item->tagImg,
                    'tag_img_link' => $item->tagImg ? set_goods_img_link($item->tagImg) : '',
                    'changeSaleType'=>$item->changeSaleType
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询二级商品详情
     * @param $args_array
     * @return null
     */
    public function searchGoodsListDetail( $args_array ) {

        $meet = 0; //符合条件的数量

        if (isset($args_array['postID']) && quanli_is_int($args_array['postID'])) {
            ++$meet;
        }

        if ($meet == 0) {
            return null;
        }

        $goods_array = $this->searchList( $args_array );
        if ($goods_array) {
            return $goods_array[0];
        } else {
            return null;
        }

    }

    /**
     * 查询三级商品信息
     * @param array $arg_array 查询参数数组
     * @return null|array
     */
    public function searchGoodsSale( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //二级网购号
        if (isset($arg_array['twoPostID']) && is_numeric($arg_array['twoPostID'])) {
            $sql .= ' AND twoPostID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['twoPostID'];
        }

        //三级网购号
        if (isset($arg_array['postID']) && is_numeric($arg_array['postID'])) {
            $sql .= ' AND postID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['postID'];
        }

        //商品SKU
        if (isset($arg_array['product_code']) && !empty($arg_array['product_code'])) {
            $sql .= ' AND product_code = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['product_code'];
        }

        //商品名称
        if (isset($arg_array['goodsName']) && !empty($arg_array['goodsName'])) {
            $sql .= ' AND goodsName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['goodsName'] . '%';
        }

        //add by allen.qiang 20150107 FUN-OT-0008-MCAKE-VIZURY接口 start
        if (isset($arg_array['bigCategoryID']) && !empty($arg_array['bigCategoryID'])) {
            $sql .= ' AND bigCategoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['bigCategoryID'];
        }
        //add by allen.qiang 20150107 FUN-OT-0008-MCAKE-VIZURY接口 end
        //商品编号
        if (isset($arg_array['mainPostID']) && !empty($arg_array['mainPostID'])) {
            $sql .= ' AND mainPostID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['mainPostID'];
        }

        //城市ID
        if (isset($arg_array['cityID']) && !empty($arg_array['cityID'])) {
            $sql .= ' AND cityID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['cityID'];
        }

        //上下架
        if (isset($arg_array['forsale']) && in_array($arg_array['forsale'], array(1, 2))) {
            $sql .= ' AND forsale = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['forsale'];
        }

        //有无库存
        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //试吃订单

        if (isset($arg_array['foretaste']) && !empty($arg_array['foretaste'])) {
            $sql .= ' AND foretaste = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['foretaste'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_sale ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM goods_sale ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            $item_array = array();
            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'mainPostID' => $item->mainPostID,
                    'postID' => $item->postID,
                    'goodsID' => $item->goodsID,
                    'product_code' => $item->product_code,
                    'twoPostID' => $item->twoPostID,
                    'goodsName' => $item->goodsName,
                    'frenchName' => $item->frenchName,
                    'bigCategoryID' => $item->bigCategoryID,
                    'bigCategoryName' => $item->bigCategoryName,
                    'midCategoryID' => $item->midCategoryID,
                    'midCategoryName' => $item->midCategoryName,
                    'smallCategoryID' => $item->smallCategoryID,
                    'smallCategoryName' => $item->smallCategoryName,
                    'marketPrice' => $item->marketPrice,
                    'salePrice' => $item->salePrice,
                    'useFlg' => $item->useFlg,
                    'forsale' => $item->forsale,
                    'foretaste' => $item->foretaste,
                    'salePoints' => $item->salePoints,
                    'description' => $item->description,
                    'spec' => $item->spec,
                    'tag' => $item->tag,
                    'chineseBrief' => $item->chineseBrief,
                    'frenchBrief' => $item->frenchBrief,
                    'goodsWidth' => $item->goodsWidth,
                    'goodsHeight' => $item->goodsHeight,
                    'goodsWeight' => $item->goodsWeight,
                    'goodsMainPic' => $item->goodsMainPic,
                    'img_link' => set_goods_img_link( $item->goodsMainPic ),
                    'link' => set_goods_link( $item->postID ),
                    'wap_link' => set_goods_link( $item->postID, 2 ),
                    'cityID' => $item->cityID,
                    'cityName' => $item->cityName,
                    'goodsLength' => $item->goodsLength,
                    'goodsPicTitle' => $item->goodsPicTitle,
                    'goodsPicAlt' => $item->goodsPicAlt,
                    'aheadTime' => $item->aheadTime,
                    'edible' => $item->edible,
                    'size' => $item->size,
                    'sizeID' => $item->sizeID,
                    'syncopate' => $item->syncopate,
                    'syncID' => $item->syncID,
                    'birthcard' => $item->birthcard,
                    'birthID' => $item->birthID,
                    'startDeliver' => $item->startDeliver,
                    'endDeliver' => $item->endDeliver,
                    'deliverSpace' => $item->deliverSpace,
                    'cutTime' => $item->cutTime,
                    'twoPostID' => $item->twoPostID,
                    'description' => $item->description,
                    'brandID' => $item->brandID,
                    'brandName' => $item->brandName
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询三级商品详情
     * @param $args_array
     * @return null
     */

    public function searchGoodsSaleDetail( $args_array ) {

        $meet = 0; //符合条件的数量

        if (isset($args_array['postID']) && quanli_is_int($args_array['postID'])) {
            ++$meet;
        }

        if (isset($args_array['product_code']) && !empty($args_array['product_code'])) {
            ++$meet;
        }

        if ($meet == 0) {
            return null;
        }

        $goods_array = $this->searchGoodsSale( $args_array );
        if ($goods_array) {

            //查询二级商品信息
            $arg_array = array(
                'postID' => $goods_array[0]['twoPostID']
            );

            $goods_list_array = $this->searchGoodsListDetail( $arg_array );
            if (!$goods_list_array) {
                return null;
            }

            $goods_array[0]['tag_img_link'] = $goods_list_array['tag_img_link'];

            return $goods_array[0];
        } else {
            return null;
        }

    }

    /**
     * 查询二级待发布商品信息
     * @param array $arg_array 查询参数数组
     * @return null|array
     */
    public function searchListEdit( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //商品SKU
        if (isset($arg_array['product_code']) && !empty($arg_array['product_code'])) {
            $sql .= ' AND product_code = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['product_code'];
        }

        //商品名称
        if (isset($arg_array['goodsName']) && !empty($arg_array['goodsName'])) {
            $sql .= ' AND goodsName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['goodsName'] . '%';
        }

        //商品编号
        if (isset($arg_array['mainPostID']) && !empty($arg_array['mainPostID'])) {
            $sql .= ' AND mainPostID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['mainPostID'];
        }
        
        //网购号
        if (isset($arg_array['postID']) && !empty($arg_array['postID'])) {
            $sql .= ' AND postID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['postID'];
        }

        //是否可销售
        if (isset($arg_array['forsale']) && in_array($arg_array['forsale'], array(0, 1))) {
            $sql .= ' AND forsale = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['forsale'];
        }

        //上下架
        if (isset($arg_array['useFlg']) && $arg_array['useFlg'] != '' && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //是否可试吃
        if (isset($arg_array['foretaste']) && in_array($arg_array['foretaste'], array(0, 1))) {
            $sql .= ' AND foretaste = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['foretaste'];
        }

        //分类
        if (isset($arg_array['bigCategoryID']) && !empty($arg_array['bigCategoryID'])) {
            $sql .= ' AND bigCategoryID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['bigCategoryID'];
        }

        //城市
        if (isset($arg_array['cityID']) && !empty($arg_array['cityID'])) {
            $sql .= ' AND cityID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['cityID'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_list ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM goods_list ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'goodsName' => $item->goodsName,
                    'frenchName' => $item->frenchName,
                    'mainPostID' => $item->mainPostID,
                    'postID' => $item->postID,
                    'goodsID' => $item->goodsID,
                    'product_code' => $item->product_code,
                    'bigCategoryID' => $item->bigCategoryID,
                    'bigCategoryName' => $item->bigCategoryName,
                    'midCategoryID' => $item->midCategoryID,
                    'midCategoryName' => $item->midCategoryName,
                    'smallCategoryID' => $item->smallCategoryID,
                    'smallCategoryName' => $item->smallCategoryName,
                    'useFlg' => $item->useFlg,
                    'forsale' => $item->forsale,
                    'foretaste' => $item->foretaste,
                    'spec' => $item->spec,
                    'tag' => $item->tag,
                    'marketPrice' => $item->marketPrice,
                    'salePrice' => $item->salePrice,
                    'discountFlg' => $item->discountFlg,
                    'chineseBrief' => $item->chineseBrief,
                    'frenchBrief' => $item->frenchBrief,
                    'goodsMainPic' => set_goods_img_link( $item->goodsMainPic ),
                    'uid' => $item->uid,
                    'isPublish' => $item->isPublish,
                    'cityID' => $item->cityID,
                    'cityName' => $item->cityName,
                    'goodsPicTitle' => $item->goodsPicTitle,
                    'goodsPicAlt' => $item->goodsPicAlt,
                    'sortOrder' => $item->sortOrder
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询三级待发布商品信息
     * @param array $arg_array 查询参数数组
     * @return null|array
     */
    public function searchSaleEdit( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //二级网购号
        if (isset($arg_array['twoPostID']) && is_numeric($arg_array['twoPostID'])) {
            $sql .= ' AND twoPostID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['twoPostID'];
        }

        //三级网购号
        if (isset($arg_array['postID']) && is_numeric($arg_array['postID'])) {
            $sql .= ' AND postID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['postID'];
        }

        //商品SKU
        if (isset($arg_array['product_code']) && !empty($arg_array['product_code'])) {
            $sql .= ' AND product_code = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['product_code'];
        }

        //商品名称
        if (isset($arg_array['goodsName']) && !empty($arg_array['goodsName'])) {
            $sql .= ' AND goodsName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['goodsName'] . '%';
        }

        //商品编号
        if (isset($arg_array['mainPostID']) && !empty($arg_array['mainPostID'])) {
            $sql .= ' AND mainPostID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['mainPostID'];
        }

        //上下架
        if (isset($arg_array['forsale']) && in_array($arg_array['forsale'], array(0, 1))) {
            $sql .= ' AND forsale = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['forsale'];
        }

        //有无库存
        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_sale ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM goods_sale ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                
                if ($item->cutTime == '00:00:00') {
                    $item->cutTime = '';
                }
                $item_array[] = array(
                    'mainPostID' => $item->mainPostID,
                    'postID' => $item->postID,
                    'goodsID' => $item->goodsID,
                    'product_code' => $item->product_code,
                    'twoPostID' => $item->twoPostID,
                    'goodsName' => $item->goodsName,
                    'frenchName' => $item->frenchName,
                    'bigCategoryID' => $item->bigCategoryID,
                    'bigCategoryName' => $item->bigCategoryName,
                    'midCategoryID' => $item->midCategoryID,
                    'midCategoryName' => $item->midCategoryName,
                    'smallCategoryID' => $item->smallCategoryID,
                    'smallCategoryName' => $item->smallCategoryName,
                    'marketPrice' => $item->marketPrice,
                    'salePrice' => $item->salePrice,
                    'useFlg' => $item->useFlg,
                    'forsale' => $item->forsale,
                    'foretaste' => $item->foretaste,
                    'salePoints' => $item->salePoints,
                    'description' => $item->description,
                    'spec' => $item->spec,
                    'tag' => $item->tag,
                    'chineseBrief' => $item->chineseBrief,
                    'frenchBrief' => $item->frenchBrief,
                    'goodsWidth' => $item->goodsWidth,
                    'goodsHeight' => $item->goodsHeight,
                    'goodsWeight' => $item->goodsWeight,
                    'goodsMainPic' => $item->goodsMainPic,
                    'cityID' => $item->cityID,
                    'cityName' => $item->cityName,
                    'goodsLength' => $item->goodsLength,
                    'goodsPicTitle' => $item->goodsPicTitle,
                    'goodsPicAlt' => $item->goodsPicAlt,
                    'aheadTime' => $item->aheadTime,
                    'aheadTimeID' => $item->aheadTimeID,
                    'edible' => $item->edible,
                    'edibleID' => $item->edibleID,
                    'size' => $item->size,
                    'sizeID' => $item->sizeID,
                    'syncopate' => $item->syncopate,
                    'syncID' => $item->syncID,
                    'birthcard' => $item->birthcard,
                    'birthID' => $item->birthID,
                    'startDeliver' => $item->startDeliver,
                    'endDeliver' => $item->endDeliver,
                    'interval' => $item->deliverSpace,
                    'cutTime' => $item->cutTime,
                    'brandID' => $item->brandID,
                    'brandName' => $item->brandName
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 根据会员等级计算商品价格及积分，同时查询商品促销活动
     * @param array $cust_rank 会员信息数组
     * @param array $goods_array 商品数组
     * @param array $promotion_need 商品数组
     * @param object $promotionGoods 促销活动类
     */
    public function shopCalculationGoodsPrice( $cust_rank, &$goods_array, $promotion_need, &$promotionGoods ) {

        /**
         * ==========================
         * 根据会员等级计算商品价格及积分
         * ==========================
         */
        $goods_array['price'] = round($goods_array['salePrice'] * $cust_rank['rate'], 2);
        /**
         * ==========================
         * 查询商品促销活动
         * ==========================
         */

        $postID = $goods_array['postID'];

        //商品促销活动
        $promotion_array = $promotionGoods->searchGoodsPromotion($postID, $promotion_need['send_date'], $promotion_need['billPromotion'], $promotion_need['cityid'], $promotion_need['payTypeID'], $promotion_need['use_card_array'], $promotion_need['use_coupon_array'], $promotion_need['paymentID']);

        if ($promotion_array) {

            foreach($promotion_array as &$p) {

                if ($p['type'] == 1) {                  //立减商品

                    $goods_array['price'] -= $p['money'];

                } else if ($p['type'] == 2) {           //特价商品

                    $goods_array['price'] = $p['money'];

                }else if( $p['type'] == 6 ){             //折扣

                    $goods_array['price'] *= $p['money'];

                }

                //价格小于0均设为0
                if ( $goods_array['price'] <= 0 ) {
                    $goods_array['price'] = 0;
                }

            }

            $goods_array['promotion'] = $promotion_array;

        } else {
            $goods_array['promotion'] = null;
        }

    }


    /**
     * 查询待发布表最大排序
     */
    public function maxListEditSort($cityID){
        $city_result = $this->dbca->stmt_query('SELECT MAX(sortOrder)+1 sort FROM goods_list_edit WHERE cityID = ?', 'i', $cityID);
        if (!$city_result || is_null($city_result[0]->sort)) {
            $sortOrder = 1;
        } else {
            $sortOrder = $city_result[0]->sort;
        }
        return $sortOrder;
    }

    /**
     * 修改商品分类排序
     * @param array 参数数组 $arg_array = array(
     * 							'postID' => 网购号
     * 							'sortOrder' => 排序
     * 						);
     */
    public function editSort( &$arg_array ){

        //网购号
        $postID = isset($arg_array['postID']) ? $arg_array['postID'] : null;

        //排序
        $sortOrder = isset($arg_array['sortOrder']) ? $arg_array['sortOrder'] : null;

        //城市
        $cityID = isset($arg_array['cityID']) ? $arg_array['cityID'] : null;

        if (!quanli_is_int($postID) || !quanli_is_int($sortOrder) || !quanli_is_int($cityID)) {
            return array('code' => 400, 'message' => '参数错误');
        }

        $check_list_edit = new entity('goods_list_edit');
        $check_list_edit->postID = $postID;
        if (!$this->dbca->fetch($check_list_edit)) {
            return array('code' => 404, 'message' => '商品信息未找到');
        }

        $sort_list_edit = new entity('goods_list_edit');
        $sort_list_edit->sortOrder = $sortOrder;
        $sort_list_edit->cityID = $cityID;
        if ($this->dbca->fetch($sort_list_edit)) {
            if ($sort_list_edit->postID != $postID) {
                return array('code' => 404, 'message' => '排序「' . $sortOrder . '」已存在');
            }
        }

        $check_list_edit->sortOrder = $sortOrder;
        $this->dbca->save($check_list_edit);

        return array('code' => 200, 'message' => '更新成功');
    }
    
    /**
     * 查询三级商品qs信息
     * @param array $arg_array 查询参数数组
     * @return null|array
     */
    public function searchGoodsQs( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //商品网购号
        if (isset($arg_array['postID']) && !empty($arg_array['postID'])) {
            $sql .= ' AND postID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['postID'];
        }
        
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_qs_relation ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM goods_qs_relation ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'postID' => $item->postID,
                    'qsName' => $item->qsName,
                    'recipe' => $item->recipe,
                    'weight' => $item->weight,
                    'noTaxCost' => $item->noTaxCost,
                    'taxCost' => $item->taxCost,
                    'checkPrice' => $item->checkPrice
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }
    
    /**
     * 待发布商品同步
     *
     * @param unknown_type $arg_array
     */
    public function synchronous( $arg_array ){

        global $G_SHOP;
    	
    	//参数检查
		if(!isset($arg_array['postID_str']) || empty($arg_array['postID_str'])) {
			return array( 'code' => 400, 'message' => '参数错误' );
		}
		
		$postArray = explode('_', $arg_array['postID_str']);
		if ($postArray) {
			$this->dbca->beginTransaction();
			try{
				foreach ($postArray as $postID) {
					if (empty($postID)) {
						continue;
					}

					$check_listgoods = new entity('goods_list_edit');
					$check_listgoods->postID = $postID;
					if (!$this->dbca->fetch($check_listgoods)) {
						throw new Exception('款号「' . $postID . '」商品未找到', -1000);
					} else {
						$check_listgoods->isPublish = 1;
						if (empty($check_listgoods->uid)) {
							throw new Exception('请保存待发布商品「' . $postID . '」的主商品信息', -1000);
						}
					}

					//是否已保存sku
					$sale_array = array(
						'twoPostID' => $check_listgoods->postID
					);

					$sale_goods = $this->searchSaleEdit( $sale_array );

					if (!$sale_goods) {
						throw new Exception('请保存待发布商品「' . $postID . '」的sku信息', -1000);
					}

					//判断上下架、是否销售、是否试吃
					$useFlg = 0;
                    $isSale = 0;
					$forsale = 0;
					$foretaste = 0;
					$goodsNum = 0;

					foreach ($sale_goods as $sale) {

                        $goodsNum++;
						
						$save_goods_sale = new entity('goods_sale');
						$save_goods_sale->postID = $sale['postID'];

						if ($this->dbca->fetch($save_goods_sale)) {
							$save_goods_sale->editor = $arg_array['creator'];
						} else {
							$save_goods_sale->creator = $arg_array['creator'];
						}

						$save_goods_sale->mainPostID = $sale['mainPostID'];
						$save_goods_sale->goodsID = $sale['goodsID'];
						$save_goods_sale->product_code = $sale['product_code'];
						$save_goods_sale->twoPostID = $sale['twoPostID'];
						$save_goods_sale->goodsName = $sale['goodsName'];
						$save_goods_sale->frenchName = $sale['frenchName'];
						$save_goods_sale->bigCategoryID = $sale['bigCategoryID'];
						$save_goods_sale->bigCategoryName = $sale['bigCategoryName'];
						$save_goods_sale->midCategoryID = $sale['midCategoryID'];
						$save_goods_sale->midCategoryName = $sale['midCategoryName'];
						$save_goods_sale->smallCategoryID = $sale['smallCategoryID'];
						$save_goods_sale->smallCategoryName = $sale['smallCategoryName'];
						$save_goods_sale->marketPrice = $sale['marketPrice'];
						$save_goods_sale->salePrice = $sale['salePrice'];
						$save_goods_sale->useFlg = $sale['useFlg'];
						$save_goods_sale->forsale = $sale['forsale'];
						$save_goods_sale->foretaste = $sale['foretaste'];
						$save_goods_sale->salePoints = $sale['salePoints'];
						$save_goods_sale->description = $sale['description'];
						$save_goods_sale->spec = $sale['spec'];
						$save_goods_sale->tag = $sale['tag'];
						$save_goods_sale->chineseBrief = $sale['chineseBrief'];
						$save_goods_sale->frenchBrief = $sale['frenchBrief'];
						$save_goods_sale->goodsWidth = $sale['goodsWidth'];
						$save_goods_sale->goodsHeight = $sale['goodsHeight'];
						$save_goods_sale->goodsWeight = $sale['goodsWeight'];
						$save_goods_sale->goodsMainPic = $sale['goodsMainPic'];
						$save_goods_sale->cityID = $sale['cityID'];
						$save_goods_sale->cityName = $sale['cityName'];
						$save_goods_sale->goodsLength = $sale['goodsLength'];
						$save_goods_sale->goodsPicTitle = $sale['goodsPicTitle'];
						$save_goods_sale->goodsPicAlt = $sale['goodsPicAlt'];
						$save_goods_sale->aheadTimeID = $sale['aheadTimeID'];
						$save_goods_sale->aheadTime = $sale['aheadTime'];
						$save_goods_sale->edibleID = $sale['edibleID'];
						$save_goods_sale->edible = $sale['edible'];
						$save_goods_sale->sizeID = $sale['sizeID'];
						$save_goods_sale->size = $sale['size'];
						$save_goods_sale->syncID = $sale['syncID'];
						$save_goods_sale->syncopate = $sale['syncopate'];
						$save_goods_sale->birthID = $sale['birthID'];
						$save_goods_sale->birthcard = $sale['birthcard'];
						$save_goods_sale->startDeliver = $sale['startDeliver'];
						$save_goods_sale->endDeliver = $sale['endDeliver'];
						$save_goods_sale->deliverSpace = $sale['interval'];
						$save_goods_sale->cutTime = $sale['cutTime'];
						$save_goods_sale->brandID = $sale['brandID'];
						$save_goods_sale->brandName = $sale['brandName'];
						$this->dbca->save($save_goods_sale);

						$useFlg += $sale['useFlg'];

                        if($sale['useFlg']==1){
                            $isSale = 1;
                        }

						if ($sale['forsale'] == 1) {	//是否销售
							$forsale++;
						}
						if ($sale['foretaste'] == 1) {	//是否试吃
							$foretaste++;
						}

                        //删除商品信息数据缓存 add by allen.qiang 2015-05-28
                        $G_SHOP->sessionCache_remove('goods_info_'.$sale['postID'],true);

                        //删除静态缓存文件 add by allen.qiang 2015-05-26 Start
                        //缓存文件地址
                        $cachehtmlfile = G_APPLICATION_BASEPATH . "shop/html_cache/goods/goods-{$sale['postID']}.html";

                        //判断是不是文件 判断文件是否存在
                        if(is_file($cachehtmlfile) && file_exists($cachehtmlfile)){
                            @unlink($cachehtmlfile);
                        }
                        //删除静态缓存文件 add by allen.qiang 2015-05-26 End

					}

                    if($useFlg > 0){
                        if($useFlg == $goodsNum*2){//全部sku已售罄
                            $useFlg = 2;
                        }elseif($isSale == 1){//部分sku上架
                            $useFlg = 1;
                        }else{
                            $useFlg = 0;
                        }
                    }

					if ($forsale > 0) {
						$forsale = 1;
					}

					if ($foretaste > 0) {
						$foretaste = 1;
					}
					
					//同步二级
					$save_goods_list = new entity('goods_list');
					$save_goods_list->postID = $postID;
					if ($this->dbca->fetch($save_goods_list)) {
						$save_goods_list->editor = $arg_array['creator'];
					} else {
						$save_goods_list->creator = $arg_array['creator'];
					}
					$save_goods_list->goodsName = $check_listgoods->goodsName;
					$save_goods_list->frenchName = $check_listgoods->frenchName;
					$save_goods_list->mainPostID = $check_listgoods->mainPostID;
					$save_goods_list->goodsID = $check_listgoods->goodsID;
					$save_goods_list->product_code = $check_listgoods->product_code;
					$save_goods_list->bigCategoryID = $check_listgoods->bigCategoryID;
					$save_goods_list->bigCategoryName = $check_listgoods->bigCategoryName;
					$save_goods_list->midCategoryID = $check_listgoods->midCategoryID;
					$save_goods_list->midCategoryName = $check_listgoods->midCategoryName;
					$save_goods_list->smallCategoryID = $check_listgoods->smallCategoryID;
					$save_goods_list->smallCategoryName = $check_listgoods->smallCategoryName;
					$save_goods_list->useFlg = $useFlg;
					$save_goods_list->forsale = $forsale;
					$save_goods_list->foretaste = $foretaste;
					$save_goods_list->spec = $check_listgoods->spec;
					$save_goods_list->tag = $check_listgoods->tag;
					$save_goods_list->marketPrice = $check_listgoods->marketPrice;
					$save_goods_list->salePrice = $check_listgoods->salePrice;
					$save_goods_list->discountFlg = $check_listgoods->discountFlg;
					$save_goods_list->chineseBrief = $check_listgoods->chineseBrief;
					$save_goods_list->frenchBrief = $check_listgoods->frenchBrief;
					$save_goods_list->description = $check_listgoods->description;
					$save_goods_list->goodsMainPic = $check_listgoods->goodsMainPic;
					$save_goods_list->uid = $check_listgoods->uid;
					$save_goods_list->isPublish = 1;
					$save_goods_list->cityID = $check_listgoods->cityID;
					$save_goods_list->cityName = $check_listgoods->cityName;
					$save_goods_list->goodsPicTitle = $check_listgoods->goodsPicTitle;
					$save_goods_list->goodsPicAlt = $check_listgoods->goodsPicAlt;
					$save_goods_list->sortOrder = $check_listgoods->sortOrder;
					$save_goods_list->tagID = $check_listgoods->tagID;
					$save_goods_list->tagName = $check_listgoods->tagName;
					$save_goods_list->tagImg = $check_listgoods->tagImg;
					$save_goods_list->brandID = $check_listgoods->brandID;
					$save_goods_list->brandName = $check_listgoods->brandName;
					$this->dbca->save($save_goods_list);
					
					//二级待发布
					$this->dbca->save($check_listgoods);



                    //同步商品配送时间 add by guojun.song start

                    $sql = "SELECT mainPostID FROM goods_list WHERE postID = '{$postID}'";

                    $result = $this->dbca->stmt_query($sql);

                    $dateTime = date('Y-m-d H:i:s');

                    if($result){

                        $mainPostID = $result[0]->mainPostID;

                        $sql = "SELECT postID FROM goods_sale WHERE mainPostID = '{$mainPostID}'";

                        $goods_sale_result = $this->dbca->stmt_query($sql);

                        if($goods_sale_result){

                            foreach($goods_sale_result as $v){

                                $sql = "SELECT * FROM goods_deliver_relation_edit WHERE postID={$v->postID}";

                                $relation_result = $this->dbca->stmt_query($sql);

                                if($relation_result){

                                    $this->dbca->stmt_query('DELETE FROM goods_deliver_relation WHERE postID = ?', 'i', $v->postID);

                                    foreach($relation_result as $row){

                                        $save_goods_deliver_relation = new entity('goods_deliver_relation');

                                        $save_goods_deliver_relation->postID = $row->postID;
                                        $save_goods_deliver_relation->creator = $row->creator;
                                        $save_goods_deliver_relation->createTime = $dateTime;
                                        $save_goods_deliver_relation->startOrderTime = $row->startOrderTime;
                                        $save_goods_deliver_relation->endOrderTime = $row->endOrderTime;
                                        $save_goods_deliver_relation->startDeliverTime = $row->startDeliverTime;
                                        $save_goods_deliver_relation->endDeliverTime = $row->endDeliverTime;

                                        $this->dbca->save($save_goods_deliver_relation);

                                    }
                                }

                            }

                        }

                    }

                    //同步商品配送时间   add by guojun.song end

				}
				$this->dbca->commit();

				return array('code' => 200, 'message' => '同步成功');
			} catch (Exception $e) {
				error_log($e->getMessage());
				$this->dbca->rollBack();
				if ($e->getCode() == -1000) {
					return array('code' => 404, 'message' => $e->getMessage());
				} else {
					return array('code' => 404, 'message' => '同步失败');
				}
			}
		} 
    }

    /**
     * 查询同款商品不同尺寸的商品
     * @param int $twoPostID 二级网购号
     * @return array|null
     */
    public function getGoodsSizeSpec( $twoPostID ) {

//        $arg_array = array(
//            'twoPostID' => $twoPostID,
//            'useFlg' => 1
//        );
        $arg_array = array(
            'twoPostID' => $twoPostID,
            'extend' => ' AND useFlg > 0',
        );
        $goods_array = $this->searchGoodsSale( $arg_array );
        if (!$goods_array) {
            return null;
        }

        $size_goods_array = array();

        foreach($goods_array as &$goods) {
            $size_goods_array[] = array(
                'postID' => $goods['postID'],
                'useFlg' => $goods['useFlg'],
                'salePrice' => $goods['salePrice'],
                'spec' => $goods['spec'],
                'edible' => $goods['edible'],
                'size' => $goods['size'],
                'aheadTime' => $goods['aheadTime']
            );
        }

        return $size_goods_array;

    }

    /**
     * 查询商品图片
     * @param int $postID 二级商品网购号
     * @return null|array
     */
    public function searchGoodsPicture( $postID ) {

        if (!isset($postID) || !quanli_is_int($postID)) {
            return null;
        }

        $search_sql = 'SELECT * FROM goods_picture WHERE postID = ? ORDER BY sortOrder ASC';
        $temp_item_array = &$this->dbca->stmt_query($search_sql, 'i', $postID);
        if (!$temp_item_array)
            return null;

        foreach($temp_item_array as &$item) {
            $item_array[] = array(
                'postID' => $item->postID,
                'pictureLink' => set_goods_img_link($item->pictureLink),
                'orgitalLink' => $item->pictureLink,
                'sortOrder' => $item->sortOrder,
                'mainFlg' => $item->mainFlg
            );
        }

        return $item_array;

    }

}
